#!/usr/bin/perl -w
#
# $Id: sqltest.pl 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#       

use strict;
use Getopt::Long;
use ConfigSQL;
use Socket;

my $sql = undef;
my $arg = int(scalar(@ARGV));
my %opt = ();

sub usage {
   die ("buzz off!\n");
}

sub print_elements {
   my @values = @_;

   printf("site: %s name: %s domain: %s community: %s model: %s ip: %s\n",
      $values[0][0], $values[0][1], $values[0][2], $values[0][3], 
      $values[0][4], inet_ntoa(pack("N", $values[0][5])));
}


sub main {
   GetOptions(\%opt,
      "help|?", "debug|d", "syslog|s", "conf|C=s", "backup|b=s") or usage();
   usage() if $opt{help};

   if ($opt{conf}) {
      my $count = 0;
      my $database = undef;
      my $hostname = undef;
      my $username = undef;
      my $password = undef;

      open(FH, $opt{conf}) or die("cannot open $opt{conf}: $!");
      while (<FH>) {
         $count++;
         next if /^\s*#/;
         next if /^\s*$/;

         chomp(my $line = $_);
         if ($line =~ /^\s*(db|host|user|pass)\s+(.*)\s*$/) {
            if ($1 eq "db") {
               chomp($database = $2);
            }
            elsif ($1 eq "host") {
               chomp($hostname = $2);
            }
            elsif ($1 eq "user") {
               chomp($username = $2);
            }
            elsif ($1 eq "pass") {
               chomp($password = $2);
            }
            else {
               close(FH);
               die("undefined token $1 in line $count");
            }
         }
      }

      close(FH);
      $sql = new ConfigSQL($database, $hostname, $username, $password);
      my $sqlQuery = sprintf("select site.name, rr.name, zone.mname, community,
         product.name, ipblock.address from device left join site on site.id =
         device.site left join product on product.id = device.product left join ipblock on ipblock.id = device.snmp_target left join rr on rr.id = device.name left join zone on zone.id = rr.zone where device.id = 67;");
      my @retval = $sql->execute($sqlQuery);
      for (my $ix = 0; $ix < 6; $ix++) {
         if ($ix eq 5) {
            print inet_ntoa(pack("N", $retval[0][$ix])), "\n";
         }
         else {
            print $retval[0][$ix], "\n";
         }
      }


      $sql->close();
   }
}

if ($arg le 1) {
   usage();
}
else {
   main();
}

